//
//  CALMealsViewController.m
//  CalCounter
//
//  Created by Le thanh Lam on 06/09/2013.
//  Copyright (c) 2013 Le thanh Lam. All rights reserved.
//

#import "CALMealsViewController.h"
#import "DateTimeView.h"
#import "DataHelper.h"
#import "EatingCell.h"
#import "CALEating.h"
#import "NSDate+Ex.h"
#import "CALAddEatingViewController.h"
#import "CALEatingViewController.h"

@interface CALMealsViewController ()<DateTimeViewDelegate>{
    
    __weak IBOutlet UITableView *_tableView;
    __weak IBOutlet DateTimeView *dateBarView;
    NSMutableArray *_eatingArray;
}

@end

@implementation CALMealsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [dateBarView refresh];
    _eatingArray = [[DataHelper sharedDatabase] getFoodsListInDate:[[DateTimeView selectDate] dateString]];
    [_tableView reloadData];
}
- (void)viewDidLoad
{
    [self.navigationItem setTitle:NSLocalizedString(@"meals", nil)];
    
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addEating:)];
    self.navigationItem.rightBarButtonItem = item;
    
    
    //add date time view

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)addEating:(id)sender{
    CALAddEatingViewController *vc = [[CALAddEatingViewController alloc] init];
    vc.eatingCount = _eatingArray.count;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDelegate, UITableViewDatasource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_eatingArray count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    EatingCell *cell = (EatingCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = (EatingCell *)[[NSBundle mainBundle] loadNibNamed:@"EatingCell" owner:nil options:nil][0];
    }
    
    CALEating *e = [_eatingArray objectAtIndex:indexPath.row];
    
    cell.timeLabel.text = e.eatingTime;
    cell.nameLabel.text = e.eatingName;
    cell.calsLabel.text = [NSString stringWithFormat:@"%1.1f",e.totalCals];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CALEatingViewController *vc = [[CALEatingViewController alloc] init];
    vc.currentEating = [_eatingArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    CALEating *e = [_eatingArray objectAtIndex:indexPath.row];
    [_eatingArray removeObject:e];
    [[DataHelper sharedDatabase] deleteEatingWithId:e.eatingId];
    [tableView reloadData];
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
    CALAddEatingViewController *vc = [[CALAddEatingViewController alloc] init];
    vc.eatingSelected = [_eatingArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - DateTimeViewDelegate
- (void)whenChangeTime:(NSDate *)_date{
    _eatingArray = [[DataHelper sharedDatabase] getFoodsListInDate:[[DateTimeView selectDate] dateString]];
    [_tableView reloadData];
}
#pragma mark -



@end
